package com.yigou.alipay.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.yigou.alipay.config.AliPayConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/index")
public class IndexController {

    @RequestMapping("/pay")
    @ResponseBody
    //参数 1.订单号 2.订单名称 3.付款金额 4. 商品描述
    public String pay(String WIDout_trade_no,
                      String WIDsubject,
                      String WIDtotal_amount,
                      String WIDbody) throws AlipayApiException {

//        AliPayConfig aliPayConfig = new AliPayConfig();

        //1）封装RSA签名
        // 参数 1.网关 2.AppID 3.私钥 4.返回数据的格式 5.编码 6.公钥 7.签名
        DefaultAlipayClient client = new DefaultAlipayClient(
                AliPayConfig.gatewayUrl,
                AliPayConfig.app_id,
                AliPayConfig.merchant_private_key,
                "json",
                AliPayConfig.charset,
                AliPayConfig.alipay_public_key,
                AliPayConfig.sign_type
                                          );

        //2)创建request请求
        AlipayTradeWapPayRequest payRequest = new AlipayTradeWapPayRequest();

        //3)封装对象
        AlipayTradeWapPayModel payModel = new AlipayTradeWapPayModel();
        //参数 1.订单号 2.订单名称 3.付款金额 4. 商品描述
        // WIDout_trade_no   WIDsubject  WIDtotal_amount  WIDbody
        payModel.setOutTradeNo(WIDout_trade_no);
        payModel.setSubject(WIDsubject);
        payModel.setTotalAmount(WIDtotal_amount);
        payModel.setBody(WIDbody);

        //设置请求参数
        payRequest.setBizModel(payModel);
        //异步回调的地址，成功页
        payRequest.setNotifyUrl(AliPayConfig.notify_url);
        //同步回调的地址，扫码支付页
        payRequest.setReturnUrl(AliPayConfig.return_url);

        // 生成表单
        String from = client.pageExecute(payRequest).getBody();
        System.out.println("生成提交表单=>"+from);
        return from;
    }
}
